<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input
                        v-model="queryInfo.query"
                        placeholder="请输入内容"
                        clearable
                        @clear="getGoodsList"
                        @keyup.enter.native="getGoodsSearch"
                    >
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsSearch"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="16">
                    <el-button type="primary" @click="goodsAdd">添加商品</el-button>
                </el-col>
            </el-row>
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称"></el-table-column>
                <el-table-column prop="goods_price" label="价格" width="80"></el-table-column>
                <el-table-column prop="goods_number" label="数量" width="80"></el-table-column>
                <el-table-column label="添加时间" width="120">
                    <template v-slot="scope">{{ scope.row.add_time | dateFormat }}</template>
                </el-table-column>
                <el-table-column label="操作" width="120">
                    <template v-slot="scope">
                        <el-button
                            type="primary"
                            size="mini"
                            icon="el-icon-edit"
                            @click="goodsEdit(scope.row)"
                        ></el-button>
                        <el-button
                            type="danger"
                            size="mini"
                            icon="el-icon-delete"
                            @click="deleteGoods(scope.row)"
                        ></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="pageSizes"
                :page-size="queryInfo.pagesize"
                :layout="pageLayout"
                :total="total"
                background
            ></el-pagination>
        </el-card>
    </div>
</template>

<script>
import config from '@/util/config'
export default {
    data() {
        return {
            queryInfo: {
                query: '',
                pagenum: config.pageNum,
                pagesize: config.pageSize
            },
            goodsList: [],
            total: 0,
            pageSizes: config.pageSizes,
            pageLayout: config.pageLayout
        }
    },
    created() {
        this.getGoodsList()
    },
    methods: {
        // 列表数据
        async getGoodsList() {
            const { data: res } = await this.$http.get('goods', {
                params: this.queryInfo
            })
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.goodsList = res.data.goods
            this.total = res.data.total
        },
        // 监听pagesize
        handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize
            this.getGoodsList()
        },
        // 监听pagenum
        handleCurrentChange(newPage) {
            this.queryInfo.pagenum = newPage
            this.getGoodsList()
        },
        // 商品搜索
        getGoodsSearch() {
            this.queryInfo.pagenum = 1
            this.getGoodsList()
        },
        // 添加商品&跳转
        goodsAdd() {
            this.$router.push('goods/add')
        },
        // 修改商品&跳转
        goodsEdit(goodsInfo) {
            this.$router.push({
                path: 'goods/add',
                query: { goods_id: goodsInfo.goods_id }
            })
            // this.$router.push({
            //     name: 'goodsadd',
            //     params: {
            //         goods_id: goodsInfo.goods_id
            //     }
            // })
        },
        // 删除商品
        async deleteGoods(goodsInfo) {
            const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                type: 'warning'
            }).catch(err => err)
            if (confirmRes !== 'confirm') return this.$message.info('取消了删除')
            const { data: res } = await this.$http.delete('goods/' + goodsInfo.goods_id)
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.$message.success(res.meta.msg)
            this.getGoodsList()
        }
    },
}
</script>

<style lang="less" scoped>
</style>